import { clear_gun, get_gun } from 'base/api';
import { msn_error, msn_info } from 'use/info';
import { s_psn, g_db } from 'db/state';
import { put_log } from 'base/ctl/log';
import { uDb, obj_str, uJob, cline } from 'sui';
import { ck_pos } from './code';

const do_inqr = (stepnum, step, umd, watch, tag="") => {
    const msn = get_gun(step);
    if (!msn) {
        watch.startWatch();
        return;
    }

    put_log("编码", msn, "扫描轴承编码");

    clear_gun();

    const herr = (e, psn, pos) => {
        msn_error(psn, pos);

        umd.setErr(`${e}, ${msn}`);
        watch.startWatch();
    };

    const [e, pos] = ck_pos(msn, false);
    if (e) {
        return herr(e, msn, pos);
    }

    const box_sn = g_db.box();

    uJob(null, "goods/psn",
        {
            line: cline(),
            k: msn,
        },
        r => {
            let psn_val = r;
            let val = [r];

            if (r.a) {
                psn_val = r.a;
                val = [r.a, r.b];
            }

            const psn = obj_str(psn_val, "sn");
            const task = obj_str(psn_val, "tech");

            s_psn(psn);

            umd.mset({
                "psn": psn,
            });

            const old_task = g_db.task();

            if (old_task && old_task !== task) {
                return herr(`此编码绑定的工艺为: ${task}, 与当前工艺不匹配`, psn, pos);
            }

            if (!old_task) {
                g_db.s_task(task);
                umd.set("task", task);
            }

            uJob(null, "goods/addpsn",
                {
                    line: cline(),
                    v: val,
                    pos: pos,
                    box: box_sn,
                    tag: tag,
                    seal: g_db.seal(),
                    lub: g_db.lub(),
                    mfc: g_db.mfc(),
                    qyd: g_db.qyd(),
                },
                r => {
                    const is_new = msn_info(psn, pos);

                    if (is_new) {
                        let num = g_db.num() + 1;
                        let sum = g_db.sum() + 1;

                        g_db.s_num(num);
                        g_db.s_sum(sum);

                        umd.mset({
                            sum: sum,
                            num: num,
                            err: "",
                        });
                    }
                    watch.startWatch();
                },
                e => herr("产品号绑定箱号出错", psn, pos));
        },
        e => herr("产品码解析出错", msn, pos));
};

const do_outqr = (stepnum, step, umd, watch) => {
    const msn = get_gun(step);
    if (!msn) {
        watch.startWatch();
        return;
    }

    put_log("编码", msn, "扫描轴承编码");

    clear_gun();

    const herr = (e) => {
        umd.setErr(`${e}, ${msn}`);
        watch.startWatch();
    };

    uJob(null, "goods/psn",
        {
            line: cline(),
            k: msn,
        },
        r => {
            let psn_val = r;
            if (r.a) {
                psn_val = r.a;
            }

            const psn = obj_str(psn_val, "sn");

            s_psn(psn);

            umd.mset({
                "psn": psn,
            });

            let info = [];
            info = umd.info;

            let f = null;

            if (info) {
                f = info.find(d => d.sn === psn);
            }

            if (!f) {
                return herr("此轴承不在出货单中");
            }

            const val = [{
                sn: psn,
                st: "out",
            }];

            const other = obj_str(psn_val, "other");
            if (other) {
                val.push({
                    sn: other,
                    st: "out",
                });
            }

            uDb(null, "psn/mmd",
                {
                    line: cline(),
                    v: val,
                },
                r => {
                    f["st"] = "out";

                    umd.setInfo(info);

                    let num = 0;
                    info.forEach(d => {
                        if (d.st === "out") {
                            num += 1;
                        }
                    })

                    g_db.s_num(num);
                    umd.mset({
                        num: num,
                        err: "",
                    });

                    watch.startWatch();
                },
                e => herr("产品号更新出错"));
        },
        e => herr("二维码解析出错"));
};

const do_out = (stepnum, step, umd, watch) => do_inqr(stepnum, step, umd, watch, "intime");

export {do_out, do_outqr, do_inqr};
